home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / IEditor / Generators / E / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-17  |  4.7 KB  |  189 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #include "DEV_IE:Generators/defs.h"
  15.  
  16. extern struct Library *LibInit   ( __A0 BPTR );
  17. extern struct Library *LibOpen   ( __D0 long, __A0 struct Library * );
  18. extern long            LibClose  ( __A0 struct Library * );
  19. extern long            LibExpunge( __A0 struct Library * );
  20.  
  21. struct Library *LibBase         = NULL;
  22.  
  23. long           *SysBase         = NULL;
  24.  
  25. struct Library *DOSBase         = NULL;
  26. struct Library *IntuitionBase   = NULL;
  27. struct Library *GfxBase         = NULL;
  28. struct Library *ReqToolsBase    = NULL;
  29. struct Library *GadToolsBase    = NULL;
  30. BPTR SegList                    = 0L;
  31.  
  32. /*
  33.  *    The Initialization routine is given only a seglist pointer.  Since
  34.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  35.  *    and return either NULL or the library pointer.  Exec has Forbid()
  36.  *    for us during the call.
  37.  */
  38.  
  39. struct Library *LibInit( __A0 BPTR segment )
  40. {
  41.  
  42.     struct Library *lib = NULL;
  43.  
  44.     static const long Vectors[] = {
  45.  
  46.     (long)ALibOpen,
  47.     (long)ALibClose,
  48.     (long)ALibExpunge,
  49.     (long)ALibReserved,
  50.  
  51.     (long)OpenFiles,
  52.     (long)CloseFiles,
  53.     (long)WriteHeaders,
  54.     (long)WriteVars,
  55.     (long)WriteData,
  56.     (long)WriteStrings,
  57.     (long)WriteChipData,
  58.     (long)WriteCode,
  59. //        (long)Config,
  60.     NULL,
  61.     -1
  62.     };
  63.  
  64.     SysBase = *(( struct Library ** )4 );
  65.  
  66.     if( DOSBase = OpenLibrary( "dos.library", 36 )) {
  67.     if( IntuitionBase = OpenLibrary( "intuition.library", 36 )) {
  68.         if( GfxBase = OpenLibrary( "graphics.library", 36 )) {
  69.         if( GadToolsBase = OpenLibrary( "gadtools.library", 36 )) {
  70.             if( ReqToolsBase = OpenLibrary( "reqtools.library", 37 )) {
  71.  
  72.             if( LibBase = lib = MakeLibrary( (APTR)Vectors, NULL, NULL, sizeof(struct Generator), NULL )) {
  73.  
  74.                 lib->lib_Node.ln_Type = NT_LIBRARY;
  75.                 lib->lib_Node.ln_Name = LibName;
  76.                 lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  77.                 lib->lib_Version      = 37;
  78.                 lib->lib_Revision     = 0;
  79.                 lib->lib_IdString     = (APTR)LibId;
  80.  
  81.                 ((struct Generator *)lib)->Ext      = "e";
  82.                 ((struct Generator *)lib)->Pattern  = "#?.e";
  83.  
  84.                 SegList = segment;
  85.  
  86.                 AddLibrary( lib );
  87.             }
  88.  
  89.             }
  90.         }
  91.         }
  92.     }
  93.     }
  94.  
  95.     return( lib );
  96. }
  97.  
  98. /*
  99.  *    Open is given the library pointer and the version request.  Either
  100.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  101.  *    Exec has Forbid() for us during the call.
  102.  */
  103.  
  104. struct Library *LibOpen( __D0 long version, __A0 struct Library *lib )
  105. {
  106.     ++lib->lib_OpenCnt;
  107.  
  108.     lib->lib_Flags &= ~LIBF_DELEXP;
  109.  
  110.     return( lib );
  111. }
  112.  
  113. /*
  114.  *    Close is given the library pointer and the version request.  Be sure
  115.  *    not to decrement the open count if already zero.  If the open count
  116.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  117.  *    and return the seglist.  Otherwise we return NULL.
  118.  *
  119.  *    Note that this routine never sets LIBF_DELEXP on its own.
  120.  *
  121.  *    Exec has Forbid() for us during the call.
  122.  */
  123.  
  124. long LibClose( __A0 struct Library *lib )
  125. {
  126.     if( lib->lib_OpenCnt && --lib->lib_OpenCnt )
  127.     return( NULL );
  128.  
  129.     if( lib->lib_Flags & LIBF_DELEXP )
  130.     return( LibExpunge( lib ));
  131.  
  132.     return( NULL );
  133. }
  134.  
  135. /*
  136.  *    We expunge the library and return the Seglist ONLY if the open count
  137.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  138.  *    flag and return NULL.
  139.  *
  140.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  141.  *    might be called from the memory allocator and thus we CANNOT DO A
  142.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  143.  *
  144.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  145.  *    therefore freeze if we called it ourselves.  As far as I can tell
  146.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  147.  *    below.
  148.  */
  149.  
  150. long LibExpunge( __A0 struct Library *lib )
  151. {
  152.     if( lib->lib_OpenCnt ) {
  153.  
  154.     lib->lib_Flags |= LIBF_DELEXP;
  155.     return( NULL );
  156.     }
  157.  
  158.     Remove( &lib->lib_Node );
  159.  
  160.     FreeMem(( char * )lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize );
  161.  
  162.     if( DOSBase ) {
  163.     CloseLibrary( DOSBase );
  164.     DOSBase = NULL;
  165.     }
  166.  
  167.     if( IntuitionBase ) {
  168.     CloseLibrary( IntuitionBase );
  169.     IntuitionBase = NULL;
  170.     }
  171.  
  172.     if( GfxBase ) {
  173.     CloseLibrary( GfxBase );
  174.     GfxBase = NULL;
  175.     }
  176.  
  177.     if( GadToolsBase ) {
  178.     CloseLibrary( GadToolsBase );
  179.     GadToolsBase = NULL;
  180.     }
  181.  
  182.     if( ReqToolsBase ) {
  183.     CloseLibrary( ReqToolsBase );
  184.     ReqToolsBase = NULL;
  185.     }
  186.  
  187.     return(( long )SegList );
  188. }
  189.